<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BUIMenu
 *
 * @author jianfeng
 */
class BUIMenu extends BUIControl {

    const menu_root_name = 'menu_root';

    //put your code here
    protected $owner = null;
    protected $id = null;
    protected $name = null;
    protected $value = null;
    protected $attributeNode = null;
    protected $rootName = 'menu_root';

    public function __construct($owner = NULL, $name = NULL) {
        if ($owner instanceof BEntity) {
            $this->owner = $owner;
        }

        if (is_string($name) && $name != NULL) {
            $this->rootName = $name;
        }
    }

    /**
     * 
     * @param type $data
     * @return BAttribute
     */
    public function getAttributeNode($data = NULL) {
        if ($this->attributeNode == NULL)
            $this->attributeNode = $this->getRoot();
        return $this->attributeNode;
    }

    public function setAttributeNode($attributeNode) {
        if ($attributeNode instanceof BAttribute) {
            $this->attributeNode = $attributeNode;
        }
    }

    public function addChildMenu($name, $displayName = NULL) {
        $this->getAttributeNode()->addChild(array('name' => $name, 'value' => $displayName));
    }

    public function removeMenu($name, $parent = null, $data = NULL) {
        if ($parent instanceof BUIMenu) {
            $parent = $this;
        }
        $parent->getAttributeNode()->removeAttribute($name);
    }

    public function addChild($child, $data = NULL) {
        if (is_string($child)) {
            $name = $child;
            $child = $this->getChild($name);
            $class = get_class($this);
            if (!$child instanceof BUIMenu) {
                $child = new $class($this->owner);
                $attNode = $this->getAttributeNode();
                /*
                  $childNode = array();
                  $childNode['name'] = $name;
                  $childNode['ownerType'] = get_class($this->owner);
                  $childNode['ownerId'] = $this->owner->getId();
                 * 
                 */
                $attNode->addChild($name);
                $cAttNode = $attNode->getChild($name);
                $child->setAttributeNode($cAttNode);
            }
        }
        if ($child instanceof BUIMenu) {
            $this->getAttributeNode()->addChildItem($child->getAttributeNode());
            return $child;
        }

        if (is_array($child)) {
            $child = $this->getAttributeNode()->addChild($child);
            return $child;
        }

        return FALSE;
    }

    public function getChildren($recursive = FALSE, $context = NULL) {
        $list = array();
        $attChildren = $this->getAttributeNode()->getChildren($context, $recursive);
        $class = get_class($this);
        foreach ($attChildren as $attr) {
            $mc = new $class($this->owner);
            $mc->setAttributeNode($attr);
            $list[$mc->getId()] = $mc;
        }
        return $list;
    }

    public function getChildrenAsLeaf($data = NULL) {
        $list = array();
        $attChildren = $this->getAttributeNode()->getChildrenAsLeaf(NULL, TRUE);
        $class = get_class($this);
        foreach ($attChildren as $attr) {
            $mc = new $class($this->owner);
            $mc->setAttributeNode($attr);
            $list[$mc->getId()] = $mc;
        }
        return $list;
    }

    /**
     * 
     * @param type $name
     * @param type $data
     * @return BUIMenu
     */
    public function getChild($name, $data = NULL) {
        $ac = $this->getAttributeNode()->getChild($name);
        $class = get_class($this);
        if ($ac instanceof BAttribute) {
            $child = new $class($this->owner);
            $child->setAttributeNode($ac);
            return $child;
        }
        return FALSE;
    }

    public function getParent($data = NULL) {
        $ac = $this->getAttributeNode()->getParent();
        $class = get_class($this);
        if ($ac instanceof BAttribute) {
            $parent = new $class($this->owner);
            $parent->setAttributeNode($ac);
            return $parent;
        }
        return FALSE;
    }

    public function getName($data = NULL) {
        if ($this->getAttributeNode() instanceof BAttribute) {
            $this->name = $this->getAttributeNode()->getName();
        }
        return $this->name;
    }

    public function getValue($data = NULL) {
        if ($this->attributeNode instanceof BAttribute) {
            $this->value = $this->getAttributeNode()->getValue();
        }
        return $this->value;
    }

    public function getId($data = NULL) {
        if ($this->attributeNode instanceof BAttribute) {
            $this->id = $this->getAttributeNode()->getId();
        }
        return $this->id;
    }

    public function getRoot($data = NULL) {
        $root = $this->owner->getAttribute($this->rootName);
        if (!$root instanceof BAttribute) {
            $this->owner->addAttribute($this->rootName, $this->rootName);
            $root = $this->owner->getAttribute($this->rootName);
        }
        if ($root instanceof BAttribute)
            return $root;

        return FALSE;
    }

    public function getMenuById($id) {
        $root = $this->getRoot();

        if (is_numeric($id)) {
            if ($id > $root->getId())
                return BAttribute::findById($id);
        }
        return $root;
    }

    public function getFullName($seperator = '.', $data = null) {
        if ($this->getAttributeNode() instanceof BAttribute) {
            $fn = $this->getAttributeNode()->getFullName($seperator, $data);
            return substr($fn, 10);
        }
        return NULL;
    }

}
